package com.cx.bank.dao;

import com.cx.bank.pojo.User;
import com.cx.bank.util.DBHelper;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/7/23 - 16:38
 */
public class BankDaoImpl implements BankDaoInterface {

    @Override
    public boolean insertUser(User user) throws SQLException {
        Connection conn = DBHelper.getConn();
        String sql = "insert into t_user(user_name,user_password,user_flag,user_type,balance) values(?,?,?,?,?)";
        List<Object> userList = new ArrayList<>();
        userList.add(user.getUserName());
        userList.add(user.getUserPassword());
        userList.add(user.getUserFlag());
        userList.add(user.getUserType());
        userList.add(user.getBalance());
        boolean insertStatus = DBHelper.updateDate(conn, sql, userList);
        //如果添加用户成功，写入日志
//        if (insertStatus) {
//            //将上面插入的用户userId查询出来
//            User userInDB = findByName(user.getUserName());
//            Log log = new Log();
//            log.setLogType("insertUser");
//            log.setLogAmount(user.getBalance());
//            log.setUserId(userInDB.getUserId());
//            LogUtil.insertLog(log);
//        }
        return insertStatus;
    }

    @Override
    public boolean updateMoney(String userName, double balance) throws SQLException {
        Connection conn = DBHelper.getConn();
        String sql = "update t_user set balance = ? where user_name = ?";
        List<Object> userList = new ArrayList<>();
        userList.add(balance);
        userList.add(userName);
        boolean updateMoneyStatus = DBHelper.updateDate(conn, sql, userList);
//        if (updateMoneyStatus){
//            //将上面插入的用户userId查询出来
//            User userInDB = findByName(userName);
//            Log log = new Log();
//            log.setLogType("updateMoney");
//            log.setLogAmount(balance);
//            log.setUserId(userInDB.getUserId());
//            LogUtil.insertLog(log);
//        }
        return updateMoneyStatus;
    }

    @Override
    public boolean updateUserFlag(String userName, int userFlag) {
        Connection conn = DBHelper.getConn();
        String sql = "update t_user set user_flag = ? where user_name = ?";
        List<Object> userList = new ArrayList<>();
        userList.add(userFlag);
        userList.add(userName);
        boolean updateUserFlagStatus = DBHelper.updateDate(conn, sql, userList);
        return updateUserFlagStatus;
    }

    @Override
    public boolean updateUser(User user) {
        Connection conn = DBHelper.getConn();
        String sql = "update t_user set user_name = ?,user_password = ?,user_flag = ?,user_type = ?,balance = ? where user_id = ?";
        List<Object> userList = new ArrayList<>();
        userList.add(user.getUserName());
        userList.add(user.getUserPassword());
        userList.add(user.getUserFlag());
        userList.add(user.getUserType());
        userList.add(user.getBalance());
        userList.add(user.getUserId());
        boolean updateUserStatus = DBHelper.updateDate(conn, sql, userList);
        return updateUserStatus;
    }

    @Override
    public boolean deleteUserByUserId(Integer userId) {
        Connection conn = DBHelper.getConn();
        String sql = "delete from t_user where user_id = ?";
        ArrayList<Object> userIdList = new ArrayList<>();
        userIdList.add(userId);
        boolean deleteUserStatus = DBHelper.updateDate(conn, sql, userIdList);
        return deleteUserStatus;
    }

    @Override
    public User findByName(String userName) throws SQLException {
        User user = null;
        Connection conn = DBHelper.getConn();
        String sql = "select * from t_user where user_name = ?";
        List<Object> userNameList = new ArrayList<>();
        userNameList.add(userName);

        ResultSet resultSet = DBHelper.queryDate(conn, sql, userNameList);
        //接受并处理返回的结果
        while (resultSet.next()) {
//            System.out.println("user_id:" + resultSet.getInt("user_id") + "    " +
//                    "user_name:" + resultSet.getString("user_name") + "    "+
//                    "user_name:" + resultSet.getString("user_password") + "    "+
//                    "user_flag:" + resultSet.getInt("user_flag") + "    "+
//                    "user_type:" + resultSet.getString("user_type") + "    "+
//                    "balance:" + resultSet.getDouble("balance"));

            int userIdInDB = resultSet.getInt("user_id");
            String userNameInDB = resultSet.getString("user_name");
            String userPasswordInDB = resultSet.getString("user_password");
            int userFlagInDB = resultSet.getInt("user_flag");
            String userTypeInDB = resultSet.getString("user_type");
            double balanceInDB = resultSet.getDouble("balance");
            user = new User(userIdInDB, userNameInDB, userPasswordInDB, userFlagInDB, userTypeInDB, balanceInDB);
        }
        return user;
    }

    @Override
    public User findUserByUserId(Integer userId) throws SQLException {
        Connection conn = DBHelper.getConn();
        String sql = "select * from t_user where user_id = ?";
        ArrayList<Object> userIdList = new ArrayList<>();
        userIdList.add(userId);
        ResultSet resultSet = DBHelper.queryDate(conn, sql, userIdList);
        User user = null;
        while (resultSet.next()){
            int userIdInDB = resultSet.getInt("user_id");
            String userNameInDB = resultSet.getString("user_name");
            String userPasswordInDB = resultSet.getString("user_password");
            int userFlagInDB = resultSet.getInt("user_flag");
            String userTypeInDB = resultSet.getString("user_type");
            double balanceInDB = resultSet.getDouble("balance");
            user = new User(userIdInDB, userNameInDB, userPasswordInDB, userFlagInDB, userTypeInDB, balanceInDB);
        }
        return user;
    }

    @Override
    public User findUser(String userName, String password) throws SQLException {
        User user = null;
        Connection conn = DBHelper.getConn();
        String sql = "select * from t_user where user_name = ? and user_password = ?";
        List<Object> userNameList = new ArrayList<>();
        userNameList.add(userName);
        userNameList.add(password);

        ResultSet resultSet = DBHelper.queryDate(conn, sql, userNameList);
        //接受并处理返回的结果
        while (resultSet.next()) {
            int userIdInDB = resultSet.getInt("user_id");
            String userNameInDB = resultSet.getString("user_name");
            String userPasswordInDB = resultSet.getString("user_password");
            int userFlagInDB = resultSet.getInt("user_flag");
            String userTypeInDB = resultSet.getString("user_type");
            double balanceInDB = resultSet.getDouble("balance");
            user = new User(userIdInDB, userNameInDB, userPasswordInDB, userFlagInDB, userTypeInDB, balanceInDB);
        }
        return user;
    }

    @Override
    public List<User> findAllUser() throws SQLException {
        Connection conn = DBHelper.getConn();
        String sql = "select * from t_user";
        ResultSet resultSet = DBHelper.queryDate(conn, sql, null);
        List<User> userList = new ArrayList<>();
        while (resultSet.next()){
            int userIdInDB = resultSet.getInt("user_id");
            String userNameInDB = resultSet.getString("user_name");
            String userPasswordInDB = resultSet.getString("user_password");
            int userFlagInDB = resultSet.getInt("user_flag");
            String userTypeInDB = resultSet.getString("user_type");
            double balanceInDB = resultSet.getDouble("balance");
            userList.add(new User(userIdInDB, userNameInDB, userPasswordInDB, userFlagInDB, userTypeInDB, balanceInDB));
        }
        return userList;
    }

    @Override
    public int findUserIdByUserName(String userName) throws SQLException {
        Connection conn = DBHelper.getConn();
        String sql = "select user_id from t_user where user_name = ?";
        List<Object> userNameList = new ArrayList<>();
        userNameList.add(userName);
        ResultSet resultSet = DBHelper.queryDate(conn, sql, userNameList);
        int userIdInDB = 0;
        while (resultSet.next()){
            userIdInDB = resultSet.getInt("user_id");
        }
        return userIdInDB;
    }

}
